<template>
  <div class="charts-container">
    <h2 style="margin-bottom: 20px;">宿舍管理系统 - 数据可视化</h2>

    <!-- 柱状图 + 饼图 -->
    <el-row :gutter="20" style="margin-bottom: 20px;">
      <!-- 柱状图 -->
      <el-col :span="12">
        <el-card shadow="hover">
          <div slot="header">各楼层入住人数统计</div>
          <div ref="barChart" style="width: 100%; height: 300px;"></div>
        </el-card>
      </el-col>

      <!-- 饼图 -->
      <el-col :span="12">
        <el-card shadow="hover">
          <div slot="header">男女学生比例分布</div>
          <div ref="pieChart" style="width: 100%; height: 300px;"></div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 折线图 -->
    <el-card shadow="hover">
      <div slot="header">每月水电费趋势</div>
      <div ref="lineChart" style="width: 100%; height: 350px;"></div>
    </el-card>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'Charts',
  mounted() {
    this.drawBarChart()
    this.drawPieChart()
    this.drawLineChart()
  },
  methods: {
    // 柱状图：各楼层入住情况
    drawBarChart() {
      const chartDom = this.$refs.barChart
      const myChart = echarts.init(chartDom)

      const option = {
        tooltip: { trigger: 'axis' },
        xAxis: {
          type: 'category',
          data: ['1楼', '2楼', '3楼', '4楼']
        },
        yAxis: { type: 'value' },
        series: [
          {
            name: '入住人数',
            type: 'bar',
            data: [28, 32, 25, 20],
            itemStyle: { color: '#409EFF' }
          }
        ]
      }

      myChart.setOption(option)
    },

    // 饼图：男女生比例
    drawPieChart() {
      const chartDom = this.$refs.pieChart
      const myChart = echarts.init(chartDom)

      const option = {
        tooltip: { trigger: 'item' },
        legend: { top: '5%', left: 'center' },
        series: [
          {
            name: '性别比例',
            type: 'pie',
            radius: '60%',
            data: [
              { value: 60, name: '男生' },
              { value: 40, name: '女生' }
            ],
            emphasis: {
              itemStyle: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' }
            },
            itemStyle: {
              borderRadius: 5,
              borderColor: '#fff',
              borderWidth: 2
            }
          }
        ]
      }

      myChart.setOption(option)
    },

    // 折线图：水电费趋势
    drawLineChart() {
      const chartDom = this.$refs.lineChart
      const myChart = echarts.init(chartDom)

      const option = {
        title: { text: '每月水电费趋势' },
        tooltip: { trigger: 'axis' },
        xAxis: {
          type: 'category',
          data: ['1月', '2月', '3月', '4月']
        },
        yAxis: { type: 'value' },
        series: [
          {
            name: '费用（元）',
            type: 'line',
            data: [120, 200, 150, 180],
            smooth: true,
            itemStyle: { color: '#F56C6C' }
          }
        ]
      }

      myChart.setOption(option)
    }
  }
}
</script>

<style scoped>
.charts-container {
  padding: 20px;
  background-color: #f9f9f9;
}

.el-card__header {
  font-weight: bold;
}
</style>